
from PyQt4 import  *
from principal import *
from registro import extensiones
from lenguaje import Lenguaje
import sys
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = QtGui.QMainWindow()
    
    def sintaxis():
        res = []
        for i in xrange(ui.listWidget.count()):
            if ui.listWidget.item(i).checkState():
                res.append(str(ui.listWidget.item(i).text().toLatin1()))
        s = str(ui.input.toPlainText().toLatin1())
        lang = Lenguaje([e for e in extensiones if each.nombre in res])
        try:
            exp = lang.parseString(s)
            ui.output.setText("Sintaxis Correcta!")
        except Exception, e:
            s = unicode(str(e),"utf-8")
            
            ui.output.setText(s)
            
    def reducir():
        res = []
        for i in xrange(ui.listWidget.count()):
            if ui.listWidget.item(i).checkState():
                res.append(str(ui.listWidget.item(i).text().toLatin1()))
        s = str(ui.input.toPlainText().toLatin1())
        lang = Lenguaje([e for e in extensiones if each.nombre in res])
        try:
            exp = lang.parseString(s)
            ui.output.setText(unicode(str(exp.reducir()),"utf-8"))
        except Exception, e:
            s = unicode(str(e),"utf-8")
            
            ui.output.setText(s)
            
    def tipar():
        res = []
        for i in xrange(ui.listWidget.count()):
            if ui.listWidget.item(i).checkState():
                res.append(str(ui.listWidget.item(i).text().toLatin1()))
        s = str(ui.input.toPlainText().toLatin1())
        lang = Lenguaje([e for e in extensiones if each.nombre in res])
        try:
            exp = lang.parseString(s)
            ui.output.setText(unicode(str(exp.tipar({})),"utf-8"))
        except Exception, e:
            s = unicode(str(e),"utf-8")
            
            ui.output.setText(s)
                
    window.Sintaxis = sintaxis
    window.Tipar= tipar
    window.Reducir = reducir
    
    ui = Ui_Principal()
    ui.setupUi(window)
    window.setMaximumSize(600,400)
    for each in extensiones:
        qq = QtGui.QListWidgetItem(each.nombre)
        qq.setFlags( QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
        qq.setCheckState( QtCore.Qt.Checked)
        ui.listWidget.addItem(qq)
        
    
    window.show()
    sys.exit(app.exec_())
    
    
        